﻿using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.UserInfos
{
    public class UpdateRequestHandler : IRequestHandler<UpdateRequestModel, ApiRestult>
    {
        private readonly IUserBaseRepository _userrepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userrepository"></param>
        public UpdateRequestHandler(IUserBaseRepository userrepository)
        {
            _userrepository = userrepository;
        }

        public async Task<ApiRestult> Handle(UpdateRequestModel request, CancellationToken cancellationToken)
        {
              ApiRestult restult = new ApiRestult();
            
            restult.isSuc = true;
            restult.Code = Restult.Success;
            restult.Message = "更新成功";

            UserInfo userinfo = new UserInfo
            {
                UserInfoId = request.UserInfoId,
                UserName = request.UserName,
                UserNick = request.UserNick,
                Password = request.Password,
                ModifyDate = DateTime.Now
            };
            await _userrepository.UpdateAsync(userinfo);
            return await Task.FromResult(restult);
        }
    }


    public class UpdateRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 用户主键
        /// </summary>
        public int UserInfoId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; } = string.Empty;

        /// <summary>
        /// 用户昵称
        /// </summary>
        public string UserNick { get; set; } = string.Empty;
        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; } = string.Empty;


    }
}

